<?php

namespace App\Admin\Controllers\System;

use App\Admin\Controllers\BaseAdminController;
use App\Models\System\CheckItem;
use App\Models\System\GoodsCategory;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;

class CheckItemController extends BaseAdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(CheckItem::with(['creator', 'banners', 'icon', 'thumb', 'parameters']), function (Grid $grid) {
            $grid->enableDialogCreate();
            $grid->disableEditButton();
            $grid->showQuickEditButton('编辑');
            $grid->setDialogFormDimensions('80%', '90%');

            $grid->model()->orderBy('order');

            $grid->column('id')->sortable();
//            $grid->column('category.name');
            $grid->column('name');
            $grid->column('icon.url')->image('', 50, 50);
            $grid->column('thumb.url')->image('', 50, 50);
//            $grid->column('content');
            $grid->column('original_price');
            $grid->column('price');
            $grid->column('is_selected')->switch();
            $grid->column('status')->switch();
            $grid->column('order')->orderable();
            $grid->column('creator.name');
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->expand();

//                $filter->equal('goods_category_id')->select(GoodsCategory::where('status', 1)->pluck('name', 'id'))->width('300px');
                $filter->like('name')->width('300px');
                $filter->equal('is_selected')->select(trans('check-item.options.is_selected'))->width('300px');
                $filter->equal('status')->select(trans('global.options.status'))->width('300px');
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new CheckItem(), function (Show $show) {
            $show->field('id');
            $show->field('goods_category_id');
            $show->field('name');
            $show->field('description');
            $show->field('content');
            $show->field('is_selected');
            $show->field('status');
            $show->field('order');
            $show->field('creator_id');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(CheckItem::with(['category', 'creator', 'banners', 'icon', 'parameters']), function (Form $form) {
            $form->select('goods_category_id')->options(GoodsCategory::where('status', 1)->pluck('name', 'id'))->required();
            $form->text('name');
            $form->textarea('description');
            $form->text('original_price');
            $form->text('price');
            $form->uploadimagepro('icon_id');
            $form->uploadimagepro('thumb_id');
            $form->uploadmultimagepro('banners');
            $form->ueditor('content');
            $form->hasMany('parameters', '报告参数', function (Form\NestedForm $form) { // , '报告参数'
                $form->text('name', '参数名');
                $form->text('aliasname', '别名');
                $form->select('type_id', '类型')->options([
                    1 => '字符串',
                    2 => '数字',
                    3 => '文本',
                ]);
                $form->switch('status', '状态')->default('1');
            })->useTable();
            $form->switch('is_selected');
            $form->switch('status')->default('1');
            $form->hidden('creator_id')->default(Admin::user()->id);
        });
    }
}
